#!/bin/bash

export PATH=/usr/local/bin
if [ -L /bin ];then
    export PATH=/bin:$PATH
else
    export PATH=/usr/bin:$PATH
fi

if [ -L /sbin ];then
    export PATH=/sbin:$PATH
else
    export PATH=/usr/sbin:$PATH
fi

[ -z "$PS1" ] && return
export CLICOLOR=1
export LSCOLORS=GxFxBxDxCxegedabagaced

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# ------------------------------------------------------------------------
command_exists() {
    command -v "$@" > /dev/null 2>&1
}

if command_exists "git" ; then
    function git_branch {
        # Check if we're in a valid git work tree and not in .git directory
        if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1 || [[ "$PWD" == *"/.git"* ]]; then
            return
        fi

        branch="`git branch 2>/dev/null | grep "^\*" | sed -e "s/^\*\ //"`"
        if [ "${branch}" != "" ];then
            if [ "${branch}" = "(no branch)" ];then
                branch="(`git rev-parse --short HEAD`...)"
            fi
            if [[ `git status --porcelain` ]] ; then
                branch=$branch" x"
            else
                branch=$branch" o"
            fi
            echo " $branch"
        fi
    }
    export PS1="\[\e[31;1m\]\u\[\e[0m\]@\[\e[33;1m\]\h\[\e[0m\]:\[\e[36;1m\]\w\[\e[0m\]\[\e[26;1m\]\$(git_branch)\[\e[0m\]\n\$ "
else
    export PS1="\[\e[31;1m\]\u\[\e[0m\]@\[\e[33;1m\]\h\[\e[0m\]:\[\e[36;1m\]\w\[\e[0m\]\n\$ "
fi

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac
stty -ixon
export GIT_DISCOVERY_ACROSS_FILESYSTEM=1

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
[ -f /usr/local/etc/bash_completion ] && bash /usr/local/etc/bash_completion

[ -f ~/.configrc ] && source ~/.configrc
